﻿package app.demo.MyTouchApp{
import flash.events.*;	
	import app.core.action.RotatableScalable2;	
	import app.core.object.TextObject;	
	import app.core.object.KeyboardObject;
	import app.core.object.ImageObject;
	
	import flash.display.*;		
	import flash.events.*;
	import flash.net.*;	
	import flash.geom.*;		
	import flash.text.*;
	import fl.controls.Label;
	import fl.controls.*;
	import fl.core.*;
	import flash.system.fscommand;
	import flash.system.*;
	
	
	
	public class keyBoard2ndPlayer extends RotatableScalable2
	{
    	
		
		
		private var clickgrabber:Shape = new Shape();				
		private var sizeX:int = 10000;
		private var sizeY:int = 10000;
		
		private var velX:Number = 0.0;
		private var velY:Number = 0.0;		
		
		private var velAng:Number = 0.0;
		
		private var friction:Number = 0.85;
		private var angFriction:Number = 0.92;
		
		var button:Button;
		private var labels:TextObject; 
		private var KB:KeyboardObject;		
		
		var buttonSkip:Button;
		var Text:String;
		var myMaster:master;
		var movie:MovieClip = new MovieClip();
		var topicLabel:Label = new Label();
		var imageBack:Loader = new Loader();
		var chooseLabel:Label = new Label();
		private var images:Array = ["darkBackground.png","shapesinahole.png","enteryourname.png","p2.png"];
		var n_txt;
		var enable:Boolean;
		//private var _dc:MovieClip;
		var topic:Loader = new Loader();
		var choose:Loader = new Loader();
		var choose2:Loader = new Loader();
		public function keyBoard2ndPlayer(m:master)
		{
			Text='';
			noMove=true;
			//dc = master.instance;
			TUIO.init(this,'localhost',3000,'',true);
            trace("MyTouchApp Initialized");
			
			myMaster=m;
			
			bringToFront = false;			
			
			//addChild(movie);
			//Background Page:
			imageBack.load(new URLRequest(images[0]));
			//imageBack.x=150;
			//imageBack.y=160;
			imageBack.scaleX=1.4;
			//imageBack.scaleY=0.8;
			addChild(imageBack);
			
			
			button=new Button();
			button.label="CONFIRM";
			button.name='buttonn';
			button.addEventListener(TouchEvent.MOUSE_DOWN,backKeyboard);
			button.x=900;
			button.y=585;
			button.width=100;
			button.height=40;
			button.enabled=false;
			enable=false;
			addChild(button);
			
			
			
			var buttonBack:Button=new Button();
			buttonBack.label="BACK";
			//buttonBack.x=850;
			//buttonBack.y=600;
			buttonBack.width=100;
			buttonBack.height=40;
			buttonBack.addEventListener(TouchEvent.MOUSE_DOWN,backMethod);
			addChild(buttonBack);
			
			var buttonExit:Button=new Button();
			buttonExit.label="EXIT";
			buttonExit.width=100;
			buttonExit.height=40;
			buttonExit.x=900;
			buttonExit.y=650;
			buttonExit.addEventListener(TouchEvent.MOUSE_DOWN,closeApp);
			addChild(buttonExit);
		
		
		//Page Title (TOPIC MATCHING):
		var myTextFormat1:TextFormat = new TextFormat();
		myTextFormat1.font = "Kristen ITC";
		myTextFormat1.size = 70;
		
		topicLabel.text = "Topic Matching";
		topicLabel.width= 600;
		topicLabel.height= 300;
		topicLabel.x=260;
		topicLabel.visible=true;
		topicLabel.setStyle("textFormat", myTextFormat1);
		//addChild(topicLabel);
		
		topic.load(new URLRequest(images[1]));
		topic.x=240;
		topic.scaleX=1.1;
		topic.scaleY=1.1;
		addChild(topic);

		//Label (Choose Your character):
		var myTextFormat2:TextFormat = new TextFormat();
		myTextFormat2.font = "Kristen ITC";
		myTextFormat2.size = 30;
		
		chooseLabel.text = "Enter your name: ";
		chooseLabel.width= 600;
		chooseLabel.height= 300;
		chooseLabel.x=60;
		chooseLabel.y=130;
		chooseLabel.visible=true;
		chooseLabel.setStyle("textFormat", myTextFormat2);
		//addChild(chooseLabel);
		
		choose.load(new URLRequest(images[2]));
		choose.x=20;
		choose.y=140;
		//choose.scaleX=1.1;
		//choose.scaleY=1.1;
		addChild(choose);
		
		choose2.load(new URLRequest(images[3]));
		choose2.x=20;
		choose2.y=120;
		//choose.scaleX=1.1;
		//choose.scaleY=1.1;
		addChild(choose2);
			
			
			
			//noScale = true;
			//noRotate = true;
			
			/*clickgrabber.graphics.beginFill(0xffffff, 0.0);
			clickgrabber.graphics.drawRect(-sizeX/2,-sizeY/2,sizeX,sizeY);
			clickgrabber.graphics.endFill();						
			
			this.addChild( clickgrabber );		
			this.addEventListener(Event.ENTER_FRAME, slide, false, 0, true);			*/
			
			/*
			var exitButton:Loader = new Loader();
			exitButton.load(new URLRequest("closeButton.png"));
			exitButton.x = 0;
			exitButton.y = 0;
			exitButton.scaleX = 1.0;
			exitButton.scaleY = 1.0;
			exitButton.alpha = 1.0;
			exitButton.addEventListener(TouchEvent.MOUSE_DOWN, this.exit);
			
			this.addChild(exitButton);
			*/
			KB= new KeyboardObject();
			KB.x=530;
			KB.y=530;
			this.addChild(KB);
			
			n_txt = new TextObject('', true);
            n_txt.name = 'TextObject_0';
            n_txt.closeButton = true;
			n_txt.x=500;
			n_txt.y=300;
			//n_txt.selectable=false;
            this.addChild(n_txt);
			
			buttonSkip = new Button;
			buttonSkip.x=860;
			buttonSkip.y=600;
			buttonSkip.width=100;
			buttonSkip.height=40;
			buttonSkip.width=140;
			buttonSkip.label="SKIP TO GAME";
			//this.addChild(buttonSkip);
			buttonSkip.addEventListener(TouchEvent.MOUSE_DOWN,skipAll);
			
			//labels = new TextObject("labels",false);
			//labels.name = 'labels';

			//this.addChild(labels);
			
			
		}
		function exit(e:Event) {
			//var subobj = new mTouchSurface();
			//subobj.name = "SurfaceHolder";
			//parent.addChild(subobj);
			//parent.removeChild(parent.getChildByName("PhotoSurface"));
		}
		private function skipAll(event:TouchEvent):void
		{
			myMaster.goToGame();
			
		}
		
		function closeApp(event:TouchEvent):void
		{
     		 fscommand("quit");    
     	}
		
		public override function released(dx:Number, dy:Number, dang:Number)
		{
			velX = dx;
			velY = dy;
			
			//trace(velX+':'+velY);
			
			velAng = dang / 2;
		}
		
		private function slide(e:Event)
		{
			if(this.state == "none")
			{		
				if(Math.abs(velX) < 0.001)
					velX = 0;
				else {
					x += velX;
					velX *= friction;										
				}
				if(Math.abs(velY) < 0.001)
					velY = 0;					
				else {
					y += velY;
					velY *= friction;						
				}
			}
		}		
		
		public function addPhoto(sz:String)
		{
			//var photo:ImageObject = new ImageObject( sz );
			//this.addChild(photo);
		}
		
		public function backMethod(e:TouchEvent):void
		{
			button.enabled=false;
			enable=false;
			n_txt.t.text='';
			myMaster.goBackToAvatarP2();
			
		}		
		public function backKeyboard(e:TouchEvent):void{
		// var curPt:Point=parent.globalToLocal(new Point(e.stageX,e.stageY));
          //  addBlob(e.ID, curPt.x, curPt.y);
            //e.stopPropagation();
			//button.alpha=0.5;
			trace(button.enabled);
			if(button.enabled==true)
			{
				enable=true;
			}
			//before doing that, transmit the name to the select topic.
			if(enable==true)
			{
			Text=n_txt.t.text;
			trace("Text is");
			trace(Text);
			n_txt.t.text='';
			myMaster.goToSelectBoard2();
			button.enabled=false;
			enable=false;
			}
			//removeChild(pageKeyboard);
			//addChild(movie0); // the Page before that one
			
			//button.upState = drawButtonState(0x4F42C6);
			//button.upState = drawButtonState(0x4F42C6);
		}
		
		
	}
}